
package org.vasil.jouikov.synchro

import com.gmongo.GMongoClient
//import com.mongodb.ServerAddress;

class DatabaseService {

    //static transactional = false
    //static addrs = [new ServerAddress("node01", 27017), new ServerAddress("node04", 27017)]

    private def db = new GMongoClient("192.168.200.101", 10001).getDB("store")

    def db() {
        db
    }

    def updateCategory = { sku, item ->

        def doc = db.categories.findOne('node0': item.category.parentid.text())
        if(!doc) { // insert new category
            db.categories.insert([
                'node0': item.category.parentid.text(),
                'name':  item.category.parentname.text(),
                'count': 1,
                'active': true,
                'nodes': [[
                    'node1': item.category.categoryid.text(),
                    'name':  item.category.catname.text(),
                    'count': 1,
                    'active': true,
                    'items':[ sku ],
                    'pref': [
                        'name1': item.category.prefname1.text(),
                        'name2': item.category.prefname2.text(),
                        'name3': item.category.prefname3.text(),
                        'name4': item.category.prefname4.text(),
                        'name5': item.category.prefname5.text()
                    ]
                ]]
            ])
        }
        else { // update

            def node1 = doc.nodes.find { it.node1 == item.category.categoryid.text() }
            if(!node1) { // insert new sub

                db.categories.update(
                    ['node0': item.category.parentid.text()],
                    ['$push': [
                        'nodes': [
                            'node1': item.category.categoryid.text(),
                            'name': item.category.catname.text(),
                            'count': 1,
                            'active': true,
                            'items':[ sku ],
                            'pref': [
                                'name1': item.category.prefname1.text(),
                                'name2': item.category.prefname2.text(),
                                'name3': item.category.prefname3.text(),
                                'name4': item.category.prefname4.text(),
                                'name5': item.category.prefname5.text()
                            ]
                        ]
                    ],
                    '$set': ['name': item.category.parentname.text()],
                    '$inc': ['count': 1] ])
            }
            else { // update sub

                def haveItem = node1.items.find { it == sku }, itemsku = [:], inc = [:], sets = [:]
                if(!haveItem) { // dont have -> insert
                    itemsku << ['nodes.$.items': sku]
                    inc     << ['count': 1, 'nodes.$.count': 1]
                }

                sets << ['name': item.category.parentname.text()]
                sets << ['nodes.$.name': item.category.catname.text()]
                sets << ['nodes.$.pref.name1': item.category.prefname1.text()]
                sets << ['nodes.$.pref.name2': item.category.prefname2.text()]
                sets << ['nodes.$.pref.name3': item.category.prefname3.text()]
                sets << ['nodes.$.pref.name4': item.category.prefname4.text()]
                sets << ['nodes.$.pref.name5': item.category.prefname5.text()]
                db.categories.update(
                    ['node0': item.category.parentid.text(), 'nodes.node1': item.category.categoryid.text()],
                    [
                        '$set':  sets,
                        '$push': itemsku,
                        '$inc':  inc
                    ]
                )
            }
        }
    }

    def updateItem = { sku, item ->

        def doc = db.items.findOne(['sku': sku], ['_id': 0])
        //println "doc=${doc}"
        if(doc) { // update
            println "${sku} update"
            //def what = whatFieldUpdate(cursor, item.getMap())
            //println "result=[\$set: ${what}]"
            //db.items.update([sku: sku], [$set: what, $inc: [upcount: 1]])
        }
        else { // insert
            println "${sku} insert"
            //db.item.insert([
                //etilid: n.etilid
                //'quantity.stock': item.qty.text(),
                //'quantity.reserv': 0,
                //'stock': item.stock.text() == '1' ? true : false
/*                'keywords': n.keywords
        if(o.weight != n.weight)                         res << [weight: n.weight]
        if(o.drone != n.drone)                           res << [drone: n.drone]
        if(o.json != n.json)                             res << [json: n.json]
        if(o.sockdate != n.sockdate)                     res << [sockdate: n.sockdate]
        if(o.active != n.active)                         res << [active: n.active]
        if(o.names.name != n.names.name)                 res << ['names.name': n.names.name]
        if(o.names.a != n.names.a)                       res << ['names.a': n.names.a]
        if(o.names.b != n.names.b)                       res << ['names.b': n.names.b]
        if(o.mfr.name != n.mfr.name)                     res << ['mfr.name': n.mfr.name]
        if(o.mfr.number != n.mfr.number)                 res << ['mfr.number': n.mfr.number]
        if(o.mfr.link != n.mfr.link)                     res << ['mfr.link': n.mfr.link]
        if(o.price.cost != n.price.cost)                 res << ['price.cost': n.price.cost]
        if(o.price.sell != n.price.sell)                 res << ['price.sell': n.price.sell]
        if(o.price.hot != n.price.hot)                   res << ['price.hot': n.price.hot]
        if(o.reseller.price1 != n.reseller.price1)       res << ['reseller.price1': n.reseller.price1]
        if(o.reseller.price2 != n.reseller.price2)       res << ['reseller.price2': n.reseller.price2]
        if(o.reseller.price3 != n.reseller.price3)       res << ['reseller.price3': n.reseller.price3]
        if(o.reseller.qty2 != n.reseller.qty2)           res << ['reseller.qty2': n.reseller.qty2]
        if(o.reseller.qty3 != n.reseller.qty3)           res << ['reseller.qty3': n.reseller.qty3]
        if(o.warranty.mask != n.warranty.mask)           res << ['warranty.mask': n.warranty.mask]
        if(o.warranty.price1 != n.warranty.price1)       res << ['warranty.price1': n.warranty.price1]
        if(o.warranty.price2 != n.warranty.price2)       res << ['warranty.price2': n.warranty.price2]
        if(o.warranty.price3 != n.warranty.price3)       res << ['warranty.price3': n.warranty.price3]
        if(o.vendor.alias != n.vendor.alias)             res << ['vendor.alias': n.vendor.alias]
        if(o.vendor.name != n.vendor.name)               res << ['vendor.name': n.vendor.name]
        if(o.pics != n.pics)                             res << [imgs: n.pics]
        if(o.packag.include != n.packag.include)         res << ['packag.include': n.packag.include]
        if(o.packag.comment != n.packag.comment)         res << ['packag.comment': n.packag.comment]
        if(o.google.group != n.google.group)             res << ['google.group': n.google.group]
        if(o.preference.p1 != n.preference.p1)           res << ['preference.p1': n.preference.p1]
        if(o.preference.p2 != n.preference.p2)           res << ['preference.p2': n.preference.p2]
        if(o.preference.p3 != n.preference.p3)           res << ['preference.p3': n.preference.p3]
        if(o.preference.p4 != n.preference.p4)           res << ['preference.p4': n.preference.p4]
        if(o.preference.p5 != n.preference.p5)           res << ['preference.p5': n.preference.p5]
        if(o.advert.name != n.advert.name)               res << ['advert.name': n.advert.name]
        if(o.advert.price != n.advert.price)             res << ['advert.price': n.advert.price]
        if(o.advert.mfrname != n.advert.mfrname)         res << ['advert.mfrname': n.advert.mfrname]
        if(o.advert.mfrnumber != n.advert.mfrnumber)     res << ['advert.mfrnumber': n.advert.mfrnumber]
        if(o.advert.keywords != n.advert.keywords)       res << ['advert.keywords': n.advert.keywords]
        if(o.advert.cnet != n.advert.cnet)               res << ['advert.cnet': n.advert.cnet]
        if(o.advert.grabber != n.advert.grabber)         res << ['advert.grabber': n.advert.grabber]
        if(o.advert.bizrate != n.advert.bizrate)         res << ['advert.bizrate': n.advert.bizrate]
        if(o.advert.nextag != n.advert.nextag)           res << ['advert.nextag': n.advert.nextag]
        if(o.advert.shopping != n.advert.shopping)       res << ['advert.shopping': n.advert.shopping]
        if(o.advert.powersource != n.advert.powersource) res << ['advert.powersource': n.advert.powersource]
        if(o.advert.froogle != n.advert.froogle)         res << ['advert.froogle': n.advert.froogle]
        if(o.advert.cj != n.advert.cj)                   res << ['advert.cj': n.advert.cj]
        if(o.advert.gadgetpage != n.advert.gadgetpage)   res << ['advert.gadgetpage': n.advert.gadgetpage]
        if(o.advert.upccode != n.advert.upccode)         res << ['advert.upccode': n.advert.upccode]
        if(o.banner.special != n.banner.special)         res << ['banner.special': n.banner.special]
        if(o.banner.closeout != n.banner.closeout)       res << ['banner.closeout': n.banner.closeout]
        if(o.banner.comingsoon != n.banner.comingsoon)   res << ['banner.comingsoon': n.banner.comingsoon]
        if(o.banner.bestprice != n.banner.bestprice)     res << ['banner.bestprice': n.banner.bestprice]
        if(o.banner.firstpage != n.banner.firstpage)     res << ['banner.firstpage': n.banner.firstpage]
        if(o.banner.catpage != n.banner.catpage)         res << ['banner.catpage': n.banner.catpage]
        if(o.banner.ebay != n.banner.ebay)               res << ['banner.ebay': n.banner.ebay]
        if(o.banner.lot != n.banner.lot)                 res << ['banner.lot': n.banner.lot]
//        if(o. != n.)       res << [: n.]

        res << [update: new Date()]*/
            //])
            //db.items << item.getMap()
        }
    }







}
